package model

import (
	"gorm.io/gorm"
	"time"
	"user-srv/basic/config"
)

type Video struct {
	Id            uint32         `gorm:"column:id;type:int UNSIGNED;primaryKey;not null;" json:"id"`
	Title         string         `gorm:"column:title;type:varchar(60);not null;" json:"title"`
	Image         string         `gorm:"column:image;type:varchar(300);not null;" json:"image"`
	VideoUrl      string         `gorm:"column:video_url;type:varchar(300);not null;" json:"video_url"`
	VideoListId   int32          `gorm:"column:video_list_id;type:int;not null;" json:"video_list_id"`
	CommentCount  int32          `gorm:"column:comment_count;type:int;not null;default:0;" json:"comment_count"`
	FavoriteCount int32          `gorm:"column:favorite_count;type:int;not null;default:0;" json:"favorite_count"`
	CreatedAt     time.Time      `gorm:"column:created_at;type:datetime(3);not null;" json:"created_at"`
	UpdatedAt     time.Time      `gorm:"column:updated_at;type:datetime(3);not null;" json:"updated_at"`
	DeletedAt     gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (v *Video) TableName() string {
	return "video"
}

func (v *Video) AddVideo() error {
	return config.DB.Create(&v).Error
}

func (v *Video) GetVideoList(page, pageSize int) (list []*Video, err error) {
	err = config.DB.Scopes(Paginate(page, pageSize)).Find(&v).Error
	return
}

func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {

		if page <= 0 {
			page = 1
		}

		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}

func (v *Video) FindVideoList(key string) (list []*Video, err error) {
	err = config.DB.Where("title like ?", key).Find(&v).Error
	return
}

func (v *Video) GetVideoListTO() (list []*Video, err error) {
	err = config.DB.Order("favorite_count desc").Find(&v).Error
	return
}

type Protobuf struct {
	Id         int32     `gorm:"column:id;type:int;primaryKey;not null;" json:"id"`
	VideoId    int32     `gorm:"column:video_id;type:int;not null;" json:"video_id"`
	ProtobufId int32     `gorm:"column:protobuf_id;type:int;not null;" json:"protobuf_id"`
	Status     int8      `gorm:"column:status;type:tinyint(1);not null;" json:"status"`
	CreatedAt  time.Time `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt  time.Time `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt  time.Time `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (p *Protobuf) TableName() string {
	return "protobuf"
}

func (p *Protobuf) AddProtobuf() error {
	return config.DB.Create(&p).Error
}

func (p *Protobuf) UpdateProtobuf() error {
	return config.DB.Updates(&p).Error
}

type Like struct {
	Id        int32          `gorm:"column:id;type:int;primaryKey;not null;" json:"id"`
	VideoId   int32          `gorm:"column:video_id;type:int;not null;" json:"video_id"`
	UserId    int32          `gorm:"column:user_id;type:int;not null;" json:"user_id"`
	CreatedAt time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (l *Like) TableName() string {
	return "like"
}

func (l *Like) AddLike() error {
	return config.DB.Create(&l).Error
}

func (l *Like) FindLike(id int) (list []*Like, err error) {
	err = config.DB.Where("user_id = ?", id).Find(&list).Error
	return
}
